package com.zhuss.controller.core;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.zhuss.bean.bo.core.user.CreateUserBo;
import com.zhuss.bean.bo.core.user.DeleteUserBo;
import com.zhuss.bean.bo.core.user.ResetPwdBo;
import com.zhuss.bean.bo.core.user.StartUserBo;
import com.zhuss.bean.bo.core.user.StopUserBo;
import com.zhuss.bean.bo.core.user.UpdateUserBo;
import com.zhuss.bean.bo.core.user.UserRoleBindBo;
import com.zhuss.common.BaseController;
import com.zhuss.common.OperateService;
import com.zhuss.core.processor.UserImport;
import com.zhuss.core.processor.UserImport2;
import com.zhuss.core.processor.UserRead;
import com.zhuss.core.user.UserOperateServiceExt;
import com.zhuss.exception.OceanException;
import com.zhuss.util.ExcelUtils;


/**
 * 用户操作 controller
 * @Author zhushanshuo
   @Date 2019-11-27 19:58
 *
 */
  
 
@RestController
@RequestMapping("/user")
public class UserOperateController extends BaseController {
	
	@Autowired
	@Qualifier("userOperateService")
	private OperateService<Void, Object> userOperateService;
	
	@Autowired
	@Qualifier("userOperateServiceExt")
	private UserOperateServiceExt userOperateServiceExt;
	
	@Autowired
    private UserImport userImport;
	
	@Autowired
    private UserImport2 userImport2;
	
	@Autowired
	private UserRead userRead;
	
	/**
	 * 创建
	 * @param bo
	 * @throws OceanException
	 */
	@ResponseBody
    @PostMapping("/create")
    public void create(@RequestBody CreateUserBo bo) throws OceanException {
		
		
		userOperateService.create(bo);
    }
	
	/**
	 * 修改
	 * @param bo
	 * @throws OceanException
	 */
	@ResponseBody
    @PostMapping("/update")
    public void update(@RequestBody UpdateUserBo bo) throws OceanException {
		
		userOperateService.update(bo);
    }
	
	/**
	 * 删除
	 * @param bo
	 * @throws OceanException
	 */
	@ResponseBody
    @PostMapping("/delete")
    public void delete(@RequestBody DeleteUserBo bo) throws OceanException {
		
		userOperateService.delete(bo);
    }
	
	/**
	 * 启用
	 * @param bo
	 * @throws OceanException
	 */
	@ResponseBody
    @PostMapping("/start")
    public void start(@RequestBody StartUserBo bo) throws OceanException {
		
		userOperateServiceExt.start(bo);
    }
	
	
	/**
	 * 停用
	 * @param bo
	 * @throws OceanException
	 */
	@ResponseBody
    @PostMapping("/stop")
    public void stop(@RequestBody StopUserBo bo) throws OceanException {
		
		userOperateServiceExt.stop(bo);
    }
	
	
	/**
	 * 给用户绑定角色
	 * @param bo
	 * @throws OceanException
	 */
	@ResponseBody
    @PostMapping("/userRoleBind")
    public void userRoleBind(@RequestBody UserRoleBindBo bo) throws OceanException {
		
		userOperateServiceExt.userRoleBind(bo);
    }
	
	
	/**
	 * 重置密码
	 * @param bo
	 * @throws OceanException
	 */
	@ResponseBody
    @PostMapping("/resetPwd")
    public void resetPwd(@RequestBody ResetPwdBo bo) throws OceanException {
		
		userOperateServiceExt.resetPwd(bo);
    }
	
	
	//excel上传
    @RequestMapping(value="/batchUpload", method = RequestMethod.POST)
    public void batchUpload(@RequestParam("file") MultipartFile file, @RequestParam("type") Integer type, 
    		 @RequestParam("week") Integer week,
    		HttpServletResponse response, HttpServletRequest request) throws Exception {
    	List<String[]> resultList = new ArrayList<String[]>();
    	System.out.println(resultList);
		try {
			resultList = ExcelUtils.readExcelFile(file, 0);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		userImport.process(resultList, response);
    }
    
    //excel上传
    @RequestMapping(value="/batchUpload2", method = RequestMethod.POST)
    public Object batchUpload2(@RequestParam(name = "file", required = false) MultipartFile file, @RequestParam("type") Integer type, 
    		HttpServletResponse response, HttpServletRequest request) throws Exception {
    	List<String[]> resultList = new ArrayList<String[]>();
    	System.out.println(resultList);
		try {
			resultList = ExcelUtils.readExcelFile(file, 0);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Object result = userImport2.process(file, resultList, type);
		return result;
    }



	
}
